

package com.dog.basics.sys.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dog.basics.bean.dto.admin.sys.AdminSysDeptAddDto;
import com.dog.basics.bean.dto.admin.sys.AdminSysDeptEditDto;
import com.dog.basics.bean.vo.admin.sys.DeptTree;
import com.dog.basics.bean.model.sys.SysDeptRelation;
import com.dog.basics.bean.vo.admin.sys.AdminSysDeptInfoVo;
import com.dog.basics.common.annotation.SysLog;
import com.dog.basics.common.util.R;
import com.dog.basics.service.sys.SysDeptRelationService;
import com.dog.basics.service.sys.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *
 * @author guonh
 * @since 2018-01-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("/admin/dept")
@Api(value = "部门管理模块", tags = "部门管理模块")
public class SysDeptController {

    private final SysDeptRelationService relationService;

    private final SysDeptService sysDeptService;

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return SysDept
     */
    @GetMapping("/{id}")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "Long")
    @ApiOperation(value = "通过ID查询", notes = "sys_dept_info")
    @PreAuthorize("@pms.hasPermission('sys_dept_info')")
    public R<AdminSysDeptInfoVo> getById(@PathVariable Long id) {
        return R.ok(sysDeptService.getObj(id));
    }

    /**
     * 返回树形菜单集合
     *
     * @return 树形菜单
     */
    @GetMapping(value = "/tree")
    @ApiOperation(value = "返回树形菜单集合", notes = "sys_dept_tree")
    @PreAuthorize("@pms.hasPermission('sys_dept_tree')")
    public R<List<DeptTree>> getTree() {
        return R.ok(sysDeptService.selectTree());
    }

    /**
     * 添加
     *
     * @param obj 实体
     * @return success/false
     */
    @SysLog("添加部门")
    @PostMapping
    @ApiOperation(value = "添加部门", notes = "sys_dept_add")
    @PreAuthorize("@pms.hasPermission('sys_dept_add')")
    public R<Boolean> save(@Valid @RequestBody AdminSysDeptAddDto obj) {
        return R.ok(sysDeptService.saveDept(obj));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return success/false
     */
    @SysLog("删除部门")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除部门", notes = "sys_dept_del")
    @PreAuthorize("@pms.hasPermission('sys_dept_del')")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "Long")
    public R<Boolean> removeById(@PathVariable Long id) {
        return R.ok(sysDeptService.removeDeptById(id));
    }

    /**
     * 编辑
     *
     * @param obj 实体
     * @return success/false
     */
    @SysLog("编辑部门")
    @ApiOperation(value = "编辑部门", notes = "sys_dept_edit")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_dept_edit')")
    public R<Boolean> update(@Valid @RequestBody AdminSysDeptEditDto obj) {
        return R.ok(sysDeptService.updateDeptById(obj));
    }

    /**
     * 查收子级列表
     *
     * @return 返回子级
     */
    @ApiOperation(value = "查收子级列表", notes = "sys_dept_getDescendantList")
    @GetMapping(value = "/getDescendantList/{deptId}")
    @ApiImplicitParam(name = "deptId", value = "祖先节点", required = true, dataType = "Long")
    @PreAuthorize("@pms.hasPermission('sys_dept_getDescendantList')")
    public R<List<SysDeptRelation>> getDescendantList(@PathVariable Long deptId) {
        return R.ok(
                relationService.list(Wrappers.<SysDeptRelation>lambdaQuery().eq(SysDeptRelation::getAncestor, deptId)));
    }

}
